package fr.cephb.operonj.tools.cmdline.remapper;

import java.util.ArrayList;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import fr.cephb.operonj.core.bio.Observed;

public class SNPInfoBinding
	extends TupleBinding<SNPInfo>
	{
	@Override
	public SNPInfo entryToObject(TupleInput input)
		{
		SNPInfo snpInfo= new SNPInfo();
		snpInfo.name = input.readString();
		snpInfo.rsId = input.readString();
		
		String obs=input.readString();
		if(obs.length()==0)
			{
			snpInfo.observed = null;
			}
		else
			{
			snpInfo.observed = new Observed(obs);
			}
		
		
		snpInfo.ncbiSeq5 = input.readString();
		obs=input.readString();
		if(obs.length()==0)
			{
			snpInfo.ncbiObserved = null;
			}
		else
			{
			snpInfo.ncbiObserved = new Observed(obs);
			}
		snpInfo.ncbiSeq3 = input.readString();
		
		int n=input.readInt();
		for(int i=0;i<n;++i)
			{
			snpInfo.ncbiHandles.add(input.readString());
			}
		snpInfo.leftHitCount=input.readInt();
		snpInfo.rightHitCount=input.readInt();
		
		n=input.readInt();
		snpInfo.positions36=new ArrayList<SNPInfo.SequenceInput>(n);
		for(int i=0;i< n;++i)
			{
			SNPInfo.SequenceInput seq= new SNPInfo.SequenceInput();
			entryToSeq(input,seq);
			snpInfo.positions36.add(seq);
			}
		
		n=input.readInt();
		snpInfo.positions37=new ArrayList<SNPInfo.SequenceOutput>(n);
		for(int i=0;i< n;++i)
			{
			SNPInfo.SequenceOutput seq= new SNPInfo.SequenceOutput();
			entryToSeq(input,seq);
			seq.comment= input.readString();
			snpInfo.positions37.add(seq);
			}
		
		return snpInfo;
		}
	
	@Override
	public void objectToEntry(SNPInfo snpInfo, TupleOutput output)
		{
		output.writeString(snpInfo.name);
		output.writeString(snpInfo.rsId);
		
		output.writeString(snpInfo.observed==null?"":snpInfo.observed.asString());
		
		output.writeString(snpInfo.ncbiSeq5);
		output.writeString(snpInfo.ncbiObserved==null?"":snpInfo.ncbiObserved.asString());
		output.writeString(snpInfo.ncbiSeq3);
		output.writeInt(snpInfo.ncbiHandles.size());
		for(String h:snpInfo.ncbiHandles)
			{
			output.writeString(h);
			}
		output.writeInt(snpInfo.leftHitCount);
		output.writeInt(snpInfo.rightHitCount);
		
		output.writeInt(snpInfo.positions36.size());
		for(SNPInfo.SequenceInput seq:snpInfo.positions36)
			{
			seqToEntry(seq,output);
			}
		output.writeInt(snpInfo.positions37.size());
		for(SNPInfo.SequenceOutput seq:snpInfo.positions37)
			{
			seqToEntry(seq,output);
			output.writeString(seq.comment);
			}
		}
	

	private void seqToEntry(SNPInfo.AbstractSequence seq, TupleOutput output)
		{
		output.writeInt(seq.buildId);
		output.writeInt(seq.chromId);
		output.writeInt(seq.start);
		output.writeInt(seq.end);
		output.writeByte(seq.strand);
		output.writeInt(seq.bound_size);
		}
	private void entryToSeq(TupleInput input,SNPInfo.AbstractSequence seq)
		{
		seq.buildId=input.readInt();
		seq.chromId=input.readInt();
		seq.start=input.readInt();
		seq.end=input.readInt();
		seq.strand=input.readByte();
		seq.bound_size=input.readInt();
		}
	}
